// app.js
const Koa = require('koa');
const schedule = require('node-schedule');

const app = new Koa();
const router = require('./router')
const { executeNewsWorkflow } = require('./controller/getDailyChinaNews')
const { executeBirthDateWorkflow } = require('./controller/getBirthDateReminder')

// 定时任务：每天21：35执行新闻联播数据
schedule.scheduleJob({ hour: 21, minute: 35 }, async () => {
    console.log('定时任务触发');
    await executeNewsWorkflow();
});

// 定时任务：每天7:00执行问候提醒
schedule.scheduleJob({ hour: 7, minute: 0 }, async () => {
    console.log('定时任务触发');
    await executeBirthDateWorkflow();
});

// 中间件
app.use(async (ctx, next) => {
    console.log(`${ctx.method} ${ctx.url}`);
    await next();
    
});

app.use(router.routes())
app.use(router.allowedMethods());

const PORT = process.env.PORT || 5678;

app.listen(PORT, () => {
    console.log(`新闻联播分析服务启动，端口: ${PORT}`);
});

module.exports = app;